/**
 * ajax同步请求处理
 * @param url     请求的url
 * @param params      请求的参数 {param1:value1,param2:value2}
 * @param successCallback(json, status)
 * @param async   是否异步
 * @param errorCallback(XMLHttpRequest, textStatus, errorThrown)
 * @param dataType  html、json、jsonp、script、text ，默认json
 */
function base_ajaxPost(url, params, successCallback, async,errorCallback, dataType){
	$.ajax({
		type: "POST",
		url: url,
		data: params,
		dataType : dataType ? dataType : "json",
		async: async ? async: true,//是否异步
		success: function(json,statu){
			try{
				if(json.code == 2003){
					layer.alert('用户未登录，或登录失效，请重新登录！', {icon: 5, title : '提示'}, function(){
						window.location.href = "login.html";
					});
				}else if(json.code > 0){
					if(typeof successCallback == 'function'){
						successCallback.call(this, json, statu);
					}else{
						layer.msg(json.msg, {time: 800});
					}
				}else if(json.code < 0){
					if(typeof errorCallback == 'function'){
						errorCallback.call(this, json, statu);
					}else{
						layer.alert(json.msg, {icon: 2, title : '警告'});
					}
				}
			}catch(e){
				layer.msg(e.message, {time: 2000});
			}
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
			if(errorCallback){
				errorCallback(XMLHttpRequest, textStatus, errorThrown);
			}else{
				if(XMLHttpRequest.responseText != ""){
					layer.alert(XMLHttpRequest.responseText, {icon: 2, title : '警告'});
				}
			}
		}
	});
};

/**
 * 填充form表单
 * @param $form      form表单
 * @param gridObj    gridObj对象
 * @param rowIndex   gridObj对象行标
 */
function base_fillForm($form, gridObj, rowIndex) {
	// get record by rowIndex: gridObj.getRecord(rowIndex)
	// get column value by record and index: gridObj.getRecordIndexValue(record, index)
	// get column value by rowIndex and index: gridObj.getColumnValue(rowIndex, index)
	var record = gridObj.getRecord(rowIndex);
	$form.find("input[type=text], input[type=password], input[type=hidden], input[type=checkbox], input[type=radio]:checked, select").each(function () {
		var input_name = $.trim($(this).attr('name'));
		if (input_name != '') {
			var input_value = gridObj.getRecordIndexValue(record, input_name);
			if ($(this).attr('type') == 'radio' || $(this).attr('type') == 'checkbox') {
				if ((',' + input_value + ',').indexOf(',' + $(this).val() + ',') > -1) {
					$.bsgrid.adaptAttrOrProp($(this), 'checked', true);
				} else {
					$.bsgrid.adaptAttrOrProp($(this), 'checked', false);
				}
			} else {
				$(this).val(input_value);
			}
		}
	});
}

function base_fillContainer($container, obj) {
	if(dic != undefined){
		dic.render($container);
	}
	$container.find("input[name], select[name], textarea[name]").each(
		function(index, formElem){
			var name = $(formElem).attr("name");
			if(name != undefined && name != null && $.trim(name) != ""){
				var value = getValue(obj, name);
				if(value != undefined && value != ""){
					$(formElem).val(value);
				}else{
					var alias = $(formElem).attr("alias");
					if(alias != undefined && alias != null && $.trim(alias) != ""){
						var alias_value = getValue(obj, alias);
						$(formElem).val(alias_value);
					}else{
						$(formElem).val("");
					}
				}
			}
		}
	);
	function getValue(obj, key) {
		var attrs = key.split("\.");
		for(var i = 0; i < attrs.length; i++){
			var name = attrs[i];
			if(obj.hasOwnProperty(name)){
				obj = obj[name];
				if(obj instanceof Array){
					obj = obj[0];
				}
			}else{
				return undefined;
			}
		}
		return obj;
	}
	if(dic != undefined){
		dic.render($container);
	}
}

/**
 * 获取$contain里面的key value字符串
 * @param $contain
 * @returns {{}}
 */
function base_getFormData($contain) {
	var data = {};
	$contain.find("input[name], select[name], textarea[name]").each(
		function(index, formElem){
			var name = $(formElem).attr("name");
			if(name != undefined && name != null && $.trim(name) != ""){
				data[name] = $(formElem).val();
			}
		}
	);
	return data;
}

/**
 * 获取url get参数值
 * @param name
 * @returns value | null
 */
function base_getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); // 匹配目标参数
	var result = window.location.search.substr(1).match(reg); // 对querystring匹配目标参数
	if (result != null) {
		return decodeURIComponent(result[2]);
	} else {
		return null;
	}
}

/**
 * 打印
 * @param $contain
 */
function base_print($contain){
	var bodyHtml = document.body.innerHTML;
	document.body.innerHTML = $contain.prop("outerHTML");
	window.print();
	document.body.innerHTML = bodyHtml;
}


